define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'material/index' + location.search,
                    add_url: 'material/add',
                    edit_url: 'material/edit',
                    del_url: 'material/del',
                    multi_url: 'material/multi',
                    import_url: 'material/import',
                    table: 'material',
                }
            });

            var table = $("#table");
            var table = $("#table");
//当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#total").text(data.total);
                $("#sum_price").text(data.sum_price);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),
                            formatter: function(value, row){

                                var type_name = row.is_look == 'no' ? value+" <i class='fa fa-bell-o'></i>" : value;
                                return type_name;
                            }},
                        {field: 'sex', title: __('Sex'), searchList: {"women":__('女'),"men":__('男')}, operate:'FIND_IN_SET', formatter: Table.api.formatter.label},
                        {field: 'birth', title: __('Birth'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'height', title: __('Height')},
                        {field: 'weight', title: __('Weight')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'user.nickname', title: __('用户名')},
                        {

                            field: 'goodstitle',
                            width: "120px",
                            title: __('康复管理'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text:function (row) {
                                        return '康复管理';
                                    },
                                    title:function (row) {
                                        return '康复管理';
                                    },
                                    classname: 'btn-dialog',
                                    url: function (row) {
                                        return 'recovery/index?uid='+row.uid;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons,
                            operate: "LIKE"
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [

                                {
                                    name: 'click',
                                    title: __('描述'),
                                    text: __('描述'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    icon: 'fa fa-leaf',
                                    url: 'material/refer',
                                },
                                {
                                    name: 'click',
                                    title: __('报告'),
                                    text: __('报告'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    icon: 'fa fa-leaf',
                                    url: 'material/report',
                                }
                                ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        report: function () {
            Controller.api.bindevent();
            $(document).on('click', '.btn-shenhelook-click', function () {
                parent.layer.closeAll();
            });
        },
        referimage: function () {
            Controller.api.bindevent();
            $(document).on('click', '.btn-shenhelook-click', function () {
                parent.layer.closeAll();
            });
            $(document).on('click', '.btn-voucherimage-click', function () {

                let content = $("#c-recontent").val();
                let id = $('#c-id').val();

                $.post('material/referimage', {
                    id,
                    content
                }, function (res) {
                    if (res.code == 1) {
                        parent.Toastr.success(res.msg);
                        parent.$(".btn-refresh").trigger("click");
                        parent.layer.closeAll();

                    } else {
                        parent.Toastr.error(res.msg);
                    }
                });
            });
        },
        refer: function () {
            Controller.api.bindevent();
            $(document).on('click', '.btn-shenhelook-click', function () {
                parent.layer.closeAll();
            });
            function playAudio(value){
                var audio = new Audio(value);

                audio.play();
            }
            $(document).on('click', '.btn-voucher-click', function () {

                let content = $("#c-recontent").val();
                let id = $('#c-id').val();

                $.post('material/referRe', {
                    id,
                    content
                }, function (res) {
                    if (res.code == 1) {
                        parent.Toastr.success(res.msg);
                        parent.$(".btn-refresh").trigger("click");
                        parent.layer.closeAll();

                    } else {
                        parent.Toastr.error(res.msg);
                    }
                });
            });
            $(document).on('click', '.btn-statusre-click', function () {
                //let id = $(this).data('id');
                /* var index = parent.layer.getFrameIndex(window.name);
                 parent.layer.close(index);
                 parent.layer.closeAll("refresh");*/
                let id = $("#c-id").val();
                // alert(id);
                parent.Fast.api.open('material/referimage?ids='+id, __('回复'),{
                    //接收传过来的参数
                    callback:function(value){
                        //在这里可以接收弹出层中使用`Fast.api.close(data)`进行回传数据
                    }
                });
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
